/**
 * 文件上传的点击事件
 */
$("body").on("change",".upload-group input[type='file']",function(event){
    var files = $(this)[0].files;
    var self = $(this);
    var file = files[0];
    var maxSize = $(this).attr("max-accept");
    if(maxSize!==undefined&&maxSize*1024*1000<file.size){
        wrong_msg("文件大小超出限制");
        return;
    }
    var regex = new RegExp($(this).attr("data-accept"))
    console.log(file.type);
    if(!regex.test(file.type)){
        $(this).val("");
        BootstrapDialog.alert({
            title:"消息提示",
            message:"文件格式不正确",
            type:BootstrapDialog.TYPE_WARNING
        });
        return;
    }
    if(file instanceof File){
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.self = self;
        reader.async = false;
        reader.file = file;
        if(/image/.test(file.type)){
            reader.onerror = function(){
                BootstrapDialog.alert({
                    title:"消息提示",
                    message:"图片加载失败...",
                    type:BootstrapDialog.TYPE_WARNING
                });
            }
            reader.onload = function(){
                self.attr("data-value","");
                this.self.parents(".form-group").next().find(".show-file").empty();
                var img = document.createElement("img");
                img.src = this.result;
                img.width = 100;
                img.height = 100;
                var del = document.createElement("a");
                del.className = "del";
                del.innerHTML = "删除";
                this.self.parents(".form-group").next().find(".show-file")[0].appendChild(img);
                this.self.parents(".form-group").next().find(".show-file")[0].appendChild(del);
            };
        }else if(/audio/.test(file.type)){
            reader.onerror = function(){
                BootstrapDialog.alert({
                    title:"消息提示",
                    message:"音频加载失败...",
                    type:BootstrapDialog.TYPE_WARNING
                });
            }
            reader.onload = function(){
                var div = this.self.parents(".form-group").next();
                div.removeClass("hide");
                var showFile = div.find(".show-file");
                showFile.empty();
                var audio  = document.createElement("audio");
                audio.src = this.result;
                audio.controls = "controls";
                audio.self = this.self;
                audio.oncanplay = function(){
                    var len = parseInt(this.duration);
                    var name = this.self.data("name");
                    this.self.parent().find("input[name='"+name+"']").remove();
                    this.self.parent().append($("<input type='hidden' name='"+name+"' value='"+len+"'/>"))
                }
                var del = document.createElement("a");
                del.className = "del";
                del.innerHTML = "删除";
                del.href = "javascript:void(0);";
                showFile[0].appendChild(audio);
                showFile[0].appendChild(del);
                $(showFile).removeClass("hide");
            }
        }
    }
});

/**
 * 删除上传的文件
 */
$("body").on("click",".form-group .show-file .del",function(){
    $(this).parents(".form-group").prev().find("input[type='file']").attr("value","");
    $(this).parents(".form-group").prev().find("input[type='file']").val("");
    $(this).parents(".form-group").prev().find("input[type='hidden']").val("");
    $(this).parent().empty();
});

$("body").on("click",".upload-btn",function(){
    $(this).parent().find("input[type='file']").click();
});

function init_image_item(url) {
    let img = document.createElement("img");
    img.src = url;
    img.width = 100;
    img.height = 100;
    let del = document.createElement("a");
    del.className = "del";
    del.innerHTML = "删除";
    $(".show-file")[0].appendChild(img);
    $(".show-file")[0].appendChild(del);
}

function image_item_re_init() {
    $('.show-file').empty();
}